<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
        <title>样机数据详情</title>
        <link rel="stylesheet" href="css/reset.css" />
        <link rel="stylesheet" href="css/platform.css" />
        <link rel="stylesheet" href="css/table.css" />
    </head>

    <body>
        <div class="header">
            <div class="u-title">分公司明细</div>
        </div>
        <div style="text-align: right;margin-bottom: 10px;">
            <button class="u-btn small export">导出</button>
        </div>
        <div id="taskTable" ms-controller="taskTable"></div>
        <script type="avalon" id="tableTask">
            <div class="tableWrapper">
                <table class="table">
                    <thead>
                        <tr>
                            <th>分公司名称</th>
                            <th>{{customFuncs.monthTh(0) | filterMonth}}</th>
                            <th>{{customFuncs.monthTh(1) | filterMonth}}</th>
                            <th>{{customFuncs.monthTh(2) | filterMonth}}</th>
                            <th>{{customFuncs.monthTh(3) | filterMonth}}</th>
                            <th>{{customFuncs.monthTh(4) | filterMonth}}</th>
                            <th>{{customFuncs.monthTh(5) | filterMonth}}</th>
                            <th>{{customFuncs.monthTh(6) | filterMonth}}</th>
                            <th>{{customFuncs.monthTh(7) | filterMonth}}</th>
                            <th>{{customFuncs.monthTh(8) | filterMonth}}</th>
                            <th>{{customFuncs.monthTh(9) | filterMonth}}</th>
                            <th>{{customFuncs.monthTh(10) | filterMonth}}</th>
                            <th>{{customFuncs.monthTh(11) | filterMonth}}</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr ms-repeat-row="sources">
                            <td>{{row.branchName}}</td>
                            <td ms-repeat-month="row.rates">{{month.rate}}%</td>
                        </tr>
                    </tbody>
                    <tfoot>
                        <tr>
                            <td colspan="14">
                                <div class="pager">
                                    <div class="first-page" ms-click="firstpage" ms-class={{firstpagestyle}}>首页</div>
                                    <div class="pre-page" ms-click="prepage" ms-class={{prepagestyle}}>上一页</div>
                                    <div class="current-page">
                                        第<input class="current-page-num" type="text" ms-duplex="currentpage" ms-input="jumppage" />页
                                        <span class="totalpage">共 {{totalpage}} 页</span>
                                    </div>
                                    <div class="next-page" ms-click="nextpage" ms-class={{nextpagestyle}}>下一页</div>
                                    <div class="last-page" ms-click="lastpage" ms-class={{lastpagestyle}}>尾页</div>
                                    <div class="messageinfo">{{startrow}}-{{endrow}} 共{{total}}条</div>
                                </div>
                            </td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </script>
        <div class="loadData"></div>
        <script src="js/jquery.min.js"></script>
        <script src="js/avalon.shim.sn.js"></script>
        <script src="js/swift-ui-table-1.1.js"></script>
        <script src="js/DataInterface/DataInterface.js"></script>
        <script>
            $(function() {
                var args = {},
                    query = location.search.substring(1),
                    pairs = query.split("&"),
                    pos = idx = argname = val = null;

                for(var i = 0; i < pairs.length; i++) {
                    pos = pairs[i];
                    idx = pos.indexOf("=");
                    if(idx == -1) continue;
                    argname = pos.substring(0, idx);
                    val = decodeURIComponent(pos.substring(idx + 1));
                    args[argname] = val;
                }

                $(".header .u-title").text(args.type == 1 ? "分公司有效率明细" : "分公司贡献率明细");
                
                $(".export").on("click",function(){
                    var exportURL = DI.downModelValidArea;  //有效率
                    if(args.type == 2) {
                        exportURL = DI.downModelContributionArea;  //贡献率
                    }
                    window.location = exportURL+"?disCode="+args.code;
                });
                //表格实例化
                var taskTable = null,
                    opts = null;

                function getMonth(successFun) {
                    $.ajax({
                        type: "post",
                        dataType: "json",
                        data: {
                            "pageSize": 10,
                            "pageNo": 1,
                            "disCode": args.code
                        },
                        url: args.type == 1 ? DI.queryModelValidArea : DI.queryModelContributionArea,
                        success: function(response) {
                            successFun(response.data.list);
                        }
                    });
                }
                ratesMonth = [{
                    rate: "",
                    saleMonth: ""
                }];
                getMonth(function(data) {
                    if(data.length > 0) {
                        ratesMonth = data[0].rates;
                    }else{
                        var last12Month = [];
                        var today = new Date();
                        today.setMonth(today.getMonth() + 1);
                        for(var i = 0; i < 12; i++) {
                            var lastMonth = today.setMonth(today.getMonth() - 1);
                            var mm = (today.getMonth() + 1) > 9 ? (today.getMonth() + 1) : "0" + (today.getMonth() + 1);
                            last12Month[i] = {
                                saleMonth: today.getFullYear() + "" + mm,
                            }
                        }
                        ratesMonth = last12Month;                        
                    }
                    opts = {
                        targetId: "taskTable",
                        templateId: "tableTask",
                        settings: {
                            url: args.type == 1 ? DI.queryModelValidArea : DI.queryModelContributionArea,
                            ajaxType: "json",
                            sourceInterface: {
                                INum: "totalSize",
                                IData: "list"
                            },
                            paginationIndexName: {
                                "peerpageRows": "pageSize",
                                "currentPage": "pageNo"
                            },
                            searchParam: {
                                "pageSize": 10,
                                "pageNo": 1,
                                "disCode": args.code
                            }
                        },
                        customFuncs: {
                            monthTh: function(m) {
                                return ratesMonth[m].saleMonth;
                            }
                        }
                    };
                    taskTable = SwiftUI.swiftTable.getInstance(opts);
                });
            });
            avalon.filters.filterMonth = function(str, args, args2) {
                return str.substr(0, 4) + "年" + str.substr(4, 2) + "月";
            };
        </script>
    </body>

</html>
